/* Author: Sheila Tabuena 
   Filename: calUtilities.js 
   Date Created: JUN-22-2009

  Contains functions to support adding events to calendar 
  
  Functions include:

    setupAddEvent(tdNode)--Sets up everything to add the 'Add Event Dialog' to element "tdNode"

    doCalForm() --Creats a dialog to add a new event to the events table

    addEvent()--Callback from Add Event dialog that does form validation and actually adds event to DB

    refreshCalendar()--refreshes the calendar view after adding a new event, currently just a placeholder
	
	getCalendar() -- function to display the calendar iframe
   */

// Sets up everything to add the "Add Event Dialog" to element "tdNode"
function setupAddEvent(tdNode)
{
	divNode = addJqNode(tdNode,"div","calForm","");
	divNode.attr("title","Add Calendar Event");
	doCalForm();
	aNode = addJqNode(tdNode,"a","dialog_link","Add Event");
	aNode.attr("class","ui-state-default ui-corner-all");

	$('#calForm').dialog({
		autoOpen: false,
		width: 500,
		buttons: {
			"Add": function() {
				addEvent();
				
			},
			"Cancel": function() {
				$(this).dialog("close");
			}
		}
	});
	$('#dialog_link').click(function(){
		$('#calForm').dialog('open');
		return false;
	});
	$("#startDate").datepicker({ altField: '#actualDate' });
  $('.selector').datepicker('option', 'defaultDate', +1);
}


// Creats a dialog to add a new event to the events table
function doCalForm() 
{
  var calNode = document.getElementById("calForm");
  var tabNode = addJqNode(calNode,"table","cft1","");
  var trNode = addJqNode(tabNode,"tr","cft2","");
  var tdNode = addJqNode(trNode,"td","cft3","Title: ");
  tdNode.attr("align","right");
  tdNode = addJqNode(trNode,"td","cft4","");
  iNode =  addJqNode(tdNode,"input","cfi1","");
  trNode = addJqNode(tabNode,"tr","cft5","");
  tdNode = addJqNode(trNode,"td","cft6","Start Date: ");
  tdNode = addJqNode(trNode,"td","cft7","");
  
  dpNode =  addJqNode(tdNode,"div","startDate","");
  
  trNode = addJqNode(tabNode,"tr","cft8","");
  tdNode = addJqNode(trNode,"td","cft9","Start Time (HH:MM): ");
  tdNode = addJqNode(trNode,"td","cft10","");
  iNode =  addJqNode(tdNode,"input","cfi3","");
  trNode = addJqNode(tabNode,"tr","cft11","");
  tdNode = addJqNode(trNode,"td","cft12","Speed: ");  
  tdNode = addJqNode(trNode,"td","cft13","");
  sNode =  addJqNode(tdNode,"select","cfi4","");
  addItems(sNode,["A - Slowest","B - Slow","D - Medium","E - Fast","F - Fastest"]);

  trNode = addJqNode(tabNode,"tr","cft15","");
  tdNode = addJqNode(trNode,"td","cft16","Hills: ");  
  tdNode = addJqNode(trNode,"td","cft17","");
  sNode =  addJqNode(tdNode,"select","cfi5","");
  addItems(sNode,["1 - Flat","2 - Some Hills","3 - More Hills","4 - Very Hilly"]);
  
  trNode = addJqNode(tabNode,"tr","cft20","");
  tdNode = addJqNode(trNode,"td","cft21","Miles: ");  
  tdNode = addJqNode(trNode,"td","cft22","");
  iNode =  addJqNode(tdNode,"input","cfi6","");
  iNode.attr("size","3");
  
  trNode = addJqNode(tabNode,"tr","cft23","");
  tdNode = addJqNode(trNode,"td","cft24","Address: ");  
  tdNode = addJqNode(trNode,"td","cft25","");
  iNode =  addJqNode(tdNode,"input","cfi7","");
  trNode = addJqNode(tabNode,"tr","cft26","");
  tdNode = addJqNode(trNode,"td","cft27","City: ");  
  tdNode = addJqNode(trNode,"td","cft28","");
  iNode =  addJqNode(tdNode,"input","cfi8","");
  trNode = addJqNode(tabNode,"tr","cft29","");
  tdNode = addJqNode(trNode,"td","cft30","State: ");  
  tdNode = addJqNode(trNode,"td","cft31","");
  iNode =  addJqNode(tdNode,"input","cfi9","");  
  iNode.attr("size","2");
  iNode.attr("maxlength","2");

  
  trNode = addJqNode(tabNode,"tr","cft40","");
  trNode.attr("valign","top");
  tdNode = addJqNode(trNode,"td","cft41","Description: ");  
  tdNode = addJqNode(trNode,"td","cft42","");
  iNode =  addJqNode(tdNode,"textarea","cfi10","");
  iNode.attr("cols","35").attr("rows","5").attr("wrap","on");
}

// Callback from Add Event dialog that does form validation and actually creates the event
function addEvent()
{
	var sql = "q=INSERT INTO events (title, startDay, startTime, speed, hills, miles, address, city, state, description) VALUES (";
  var iNode = document.getElementById("cfi1");
  input = addslashes(iNode.value);
  if (input == null || input.length == 0)
  {
    iNode.value = "enter a title";
    return;
  }
  else 
  	sql += "'" + input + "', ";
 
  input = $.datepicker.formatDate('yy-mm-dd', $('#startDate').datepicker('getDate'));
 	sql += "'" + input + "', ";
  iNode = document.getElementById("cfi3");
  input = iNode.value;
  if (input == null || input.length == 0)
  {
    iNode.value = "enter start time";
    return;
  }
  almostValidTime = /^\d{1}:[0-5]\d$/;
  validTime = /^[0-2]\d:[0-5]\d$/;
  if (almostValidTime.test(input))
  {
     input = "0" + input;
  }
  if (!validTime.test(input))
  {
    iNode.value = "enter start time";
    return;
  }
  else
  	sql += "'" + input + ":00" + "', ";
  		
  var choice = $("#cfi4 option:selected");        
	input = choice.text()[0];
  sql += "'" + input + "', ";
  
  var choice = $("#cfi5 option:selected");        
	input = choice.text()[0];
  sql += "'" + input + "', ";

  iNode = document.getElementById("cfi6");
  input = iNode.value;
  if (input == null || input.length == 0)
  {
    iNode.value = "?";
    return;
  }
  else 
  	sql += "'" + input + "', ";
  iNode = document.getElementById("cfi7");
  input = addslashes(iNode.value);
  if (input == null || input.length == 0)
  {
    iNode.value = "enter address";
    return;
  }
  else 
  	sql += "'" + input + "', ";
  iNode = document.getElementById("cfi8");
  input = addslashes(iNode.value);
  if (input == null || input.length == 0)
  {
    iNode.value = "enter city";
    return;
  }
  else 
  	sql += "'" + input + "', ";
	
  iNode = document.getElementById("cfi9");
  input = iNode.value.toUpperCase();
  if (input == null || input.length == 0)
  {
    iNode.value = "CA";
  }
  sql += "'" + input + "', ";
	
  iNode = document.getElementById("cfi10");
  input = addslashes(iNode.value);
  if (input == null || input.length == 0)
  {
    iNode.value = "add description";
    return;
  }
  else 
  	sql += "'" + input + "')";
	url = "http://ctislab.fhda.edu/~tabuenas/php/anyQuery.php";
  makeAndSendQuery(url,refreshCalendar,sql);
  $('#calForm').dialog("close");
		
}
// refreshes the calendar view after adding a new event - Julie Hu
function refreshCalendar() 
{
	//window.location.reload();
	var date = new Date();
	var start = date.getFullYear() + '-01-01';
	var end = date.getFullYear() + '-12-31';
	getEventRange(start,end,displayCalendar);	
	
}

// after the drop down menu is there, this changes the options on "selectNode" to be items
function addItems(selectNode,items)
{
//  selectNode.options.length = 0; // get rid of old options in the select node
  for ( i = 0; i < items.length; ++i)
  {
    addJqNode (selectNode, "option", items[i], items[i]);
  }
}

// Loads calendar - Julie Hu
function getCalendar() {
  $(function() {
	var divNode = $('#home');
	$('#home').append(setupAddEvent(divNode));
	var $frame = $('<iframe style="width:620px; height:600px; border:0" src="calendar/calendar.html" id="calendarFrame" scrolling="no" frameborder=0>');	
    $('#home').append( $frame );
  }); 
}
